package com.szewec.ps.myview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.szewec.ps.R;
import com.szewec.ps.base.Constants;
import com.szewec.ps.ui.common.LocationEditActivity;
import com.szewec.ps.util.StringUtils;
import com.szewec.ps.util.map.MapUtils;

/**
 * 选择定位view
 */
public class LocationEdit extends ConstraintLayout implements BaseInput {
    protected TextView tvShow;
    protected TextView tv_itemName;
    protected Double xCoor;
    protected Double yCoor;
    protected Context context;
    public LocationEdit(Context context) {
        super(context);
    }

    public LocationEdit(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public LocationEdit(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.location_edit, this);
        this.context = context;
        tvShow = findViewById(R.id.tv_show);
        tv_itemName = findViewById(R.id.tv_item_name);
        tvShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, LocationEditActivity.class);
                ((Activity)context).startActivityForResult(intent, Constants.REQUEST_CODE.NOT_NEED);
            }
        });
        parseStyle(context, attrs);
//        getAddress(null,null);
    }

    public void setRequestCode(int index){
        tvShow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, LocationEditActivity.class);
                ((Activity)context).startActivityForResult(intent, index);
            }
        });
    }

    private void parseStyle(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LocationEdit);
            String name = ta.getString(R.styleable.LocationEdit_preItemName);
            name = StringUtils.isEmpty(name) ? "坐标" : name;
            tv_itemName.setText(name);
            ta.recycle();
        }
    }

    public void setValue(String value){
        if (StringUtils.isNotEmpty(value)){
            tvShow.setText(value);
            tvShow.setTextColor(getResources().getColor(R.color.C333333));
        }
    }

    public void setValue(Double x,Double y){
        if (x != null && y != null){
            getAddress(x,y);
        }else {
            tvShow.setText("暂无坐标");
        }
    }

    @Override
    public void switchoverMode(boolean isEnabled) {
        tvShow.setEnabled(isEnabled);
    }

    /**
     * 尝试获取位置
     */
    public void getAddress(Double x,Double y){
        try {
            LatLng latLng;
            if (x != null && y != null){
                latLng = new LatLng(y,x);
            }else {
                tvShow.setText("定位中...");
                Double[] coordinate = MapUtils.getCoordinate();
                latLng = new LatLng(coordinate[1],coordinate[0]);
            }
            GeoCoder geoCoder = GeoCoder.newInstance();
            geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
                @Override
                public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

                }

                @Override
                public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                    if (null == reverseGeoCodeResult){
                        return;
                    }else {
                        setValue(reverseGeoCodeResult.getAddress());
                    }
                }
            });

            geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng).radius(100));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public Double getxCoor() {
        return xCoor;
    }

    public Double getyCoor() {
        return yCoor;
    }

    public String getAddress(){
        return tvShow.getText().toString();
    }
}
